package kz.simplex.photobox.model;
// Generated Jul 19, 2009 4:45:38 PM by Hibernate Tools 3.2.2.GA

import static javax.persistence.GenerationType.IDENTITY;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.validator.Length;
import org.hibernate.validator.NotNull;

/**
 * Users generated by hbm2java
 */
@Entity
@Table(name = "users", catalog = "photobox")
public class User implements java.io.Serializable {
	private Integer id;
	private String username;
	private String password;
	private String email;
	private String firstname;
	private String lastname;
	private Sex sex = Sex.MALE;
	private Date birthdate;
	private String dsc;
	private Date registered;
	private Date lastloggedin;
	private Role role;
	private Status status;
	private byte[] photo;
	private String photoImageType;
		
	private Set<Friendship> friendshipsIApplied;
	private Set<Friendship> friendshipsAppliedToMe;

	public User() {
	}

	public User(String username, String password, String email,
			Date registered, Date lastloggedin, Role role, Status status) {
		this.username = username;
		this.password = password;
		this.email = email;
		this.registered = registered;
		this.lastloggedin = lastloggedin;
		this.role = role;
		this.status = status;
	}
	public User(String username, String password, String email,
			String firstname, String lastname, String dsc, Date registered,
			Date lastloggedin, Role role, Status status) {
		this.username = username;
		this.password = password;
		this.email = email;
		this.firstname = firstname;
		this.lastname = lastname;
		this.dsc = dsc;
		this.registered = registered;
		this.lastloggedin = lastloggedin;
		this.role = role;
		this.status = status;
	}

	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id", unique = true, nullable = false)
	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(name = "username", nullable = false, length = 32)
	@NotNull
	@Length(max = 32)
	public String getUsername() {
		return this.username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Column(name = "password", nullable = false, length = 256)
	@NotNull
	@Length(max = 256)
	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Column(name = "email", nullable = false, length = 64)
	@NotNull
	@Length(max = 64)
	public String getEmail() {
		return this.email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Column(name = "firstname", length = 64)
	@Length(max = 64)
	public String getFirstname() {
		return this.firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	@Column(name = "lastname", length = 64)
	@Length(max = 64)
	public String getLastname() {
		return this.lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}
	
	@Column(name = "sex")	
	public Sex getSex() {
		return sex;
	}
	
	public void setSex(Sex sex) {
		this.sex = sex;
	}
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "birthdate", nullable=true, length=19)
	public Date getBirthdate() {
		return birthdate;
	}
	
	public void setBirthdate(Date birthdate) {
		this.birthdate = birthdate;
	}

	@Column(name = "dsc", length = 65535)
	@Length(max = 65535)
	public String getDsc() {
		return this.dsc;
	}

	public void setDsc(String dsc) {
		this.dsc = dsc;
	}
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "registered", nullable = false, length = 19)
	@NotNull
	public Date getRegistered() {
		return this.registered;
	}

	public void setRegistered(Date registered) {
		this.registered = registered;
	}
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "lastloggedin", nullable = false, length = 19)
	@NotNull
	public Date getLastloggedin() {
		return this.lastloggedin;
	}

	public void setLastloggedin(Date lastloggedin) {
		this.lastloggedin = lastloggedin;
	}

	@ManyToOne
	@JoinColumn(name="role", referencedColumnName="id", nullable = false)
	public Role getRole() {
		return this.role;
	}
	public void setRole(Role role) {
		this.role = role;
	}
	
	@ManyToOne
	@JoinColumn(name="status", referencedColumnName="id", nullable = false)
	public Status getStatus() {
		return this.status;
	}
	public void setStatus(Status status) {
		this.status = status;
	}
	
	@Column(name="photo")
	@Lob
	@Basic(fetch=FetchType.LAZY)
	public byte[] getPhoto() {
		return this.photo;
	}
	public void setPhoto(byte[] photo) {
		this.photo = photo;
	}
	
	@Column(name="photo_image_type", length=60)
	@Length(max=60)
	public String getPhotoImageType(){
		return this.photoImageType;
	}
	public void setPhotoImageType(String photoImageType) {
		this.photoImageType = photoImageType;
	}
		
	@OneToMany(mappedBy="user1")
	public Set<Friendship> getFriendshipsIApplied() {
		return friendshipsIApplied;
	}
	public void setFriendshipsIApplied(Set<Friendship> friendshipsIApplied) {
		this.friendshipsIApplied = friendshipsIApplied;
	}

	@OneToMany(mappedBy="user2")
	public Set<Friendship> getFriendshipsAppliedToMe() {
		return friendshipsAppliedToMe;
	}
	public void setFriendshipsAppliedToMe(Set<Friendship> friendshipsAppliedToMe) {
		this.friendshipsAppliedToMe = friendshipsAppliedToMe;
	}

	@Transient
	public Set<Friendship> getAllFrinedships() {
		Set<Friendship> allFriendships = getFriendshipsIApplied();
		allFriendships.addAll(getFriendshipsAppliedToMe());
		return allFriendships;
	}
	
	@Transient
	public List<User> getAllFriends(){
		List<User> allFriends = new ArrayList<User>();
		for(Friendship f : getAllFrinedships()){
			if(f.isConfirmed()){
				allFriends.add(f.getUser1().getId() == getId() ? f.getUser2() : f.getUser1());
			}
		}
		return allFriends;
	}
}
